package com.member.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.member.common.utils.DateUtils;
import com.member.shop.domain.SysBizCouponGoods;
import com.member.shop.domain.SysBizGoods;
import com.member.shop.domain.SysBizGoodsSku;
import com.member.shop.dto.SysBizGoodsDto;
import com.member.shop.mapper.SysBizCouponGoodsMapper;
import com.member.shop.mapper.SysBizGoodsSkuMapper;
import com.member.shop.service.ISysBizCouponGoodsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 优惠券指定商品Service业务层处理
 *
 * @author lixinchen
 * @date 2024-05-08
 */
@Service
public class SysBizCouponGoodsServiceImpl extends ServiceImpl<SysBizCouponGoodsMapper, SysBizCouponGoods> implements ISysBizCouponGoodsService {
    @Resource
    private SysBizCouponGoodsMapper sysBizCouponGoodsMapper;
    @Resource
    private SysBizGoodsSkuMapper sysBizGoodsSkuMapper;

    /**
     * 查询优惠券指定商品
     *
     * @param id 优惠券指定商品主键
     * @return 优惠券指定商品
     */
    @Override
    public SysBizCouponGoods selectSysBizCouponGoodsById(Long id) {
        return sysBizCouponGoodsMapper.selectById(id);
    }

    /**
     * 查询优惠券指定商品列表
     *
     * @param queryWrapper 优惠券指定商品
     * @return 优惠券指定商品
     */
    @Override
    public List<SysBizCouponGoods> selectSysBizCouponGoodsList(LambdaQueryWrapper<SysBizCouponGoods> queryWrapper) {
        return sysBizCouponGoodsMapper.selectList(queryWrapper);
    }

    @Override
    public List<SysBizCouponGoods> selectSysBizCouponGoodsList(SysBizCouponGoods sysBizCouponGoods) {
        return sysBizCouponGoodsMapper.selectSysBizCouponGoodsList(sysBizCouponGoods);
    }

    /**
     * 新增优惠券指定商品
     *
     * @param sysBizCouponGoods 优惠券指定商品
     * @return 结果
     */
    @Override
    public int insertSysBizCouponGoods(SysBizCouponGoods sysBizCouponGoods) {
        sysBizCouponGoods.setCreateTime(DateUtils.getNowDate());
        return sysBizCouponGoodsMapper.insert(sysBizCouponGoods);
    }

    /**
     * 修改优惠券指定商品
     *
     * @param sysBizCouponGoods 优惠券指定商品
     * @return 结果
     */
    @Override
    public int updateSysBizCouponGoods(SysBizCouponGoods sysBizCouponGoods) {
        sysBizCouponGoods.setUpdateTime(DateUtils.getNowDate());
        return sysBizCouponGoodsMapper.updateById(sysBizCouponGoods);
    }

    /**
     * 批量删除优惠券指定商品
     *
     * @param ids 需要删除的优惠券指定商品主键
     * @return 结果
     */
    @Override
    public int deleteSysBizCouponGoodsByIds(Long[] ids) {
        return sysBizCouponGoodsMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除优惠券指定商品信息
     *
     * @param id 优惠券指定商品主键
     * @return 结果
     */
    @Override
    public int deleteSysBizCouponGoodsById(Long id) {
        return sysBizCouponGoodsMapper.deleteById(id);
    }

    /**
     * 删除优惠券指定商品信息
     *
     * @param couponId
     * @return 结果
     */
    @Override
    public int deleteSysBizCouponGoodsBycouponId(Long couponId) {
        return sysBizCouponGoodsMapper.deleteSysBizCouponGoodsBycouponId(couponId);
    }

    /**
     * 查询优惠券指定商品
     *
     * @param couponId
     * @return 优惠券指定商品
     */
    @Override
    public List<SysBizGoodsDto> selectSysBizCouponGoodsByCouponId(Long couponId) {
        List<SysBizGoodsDto> sysBizGoods1 = sysBizCouponGoodsMapper.selectSysBizCouponGoodsByCouponId(couponId);
        if (sysBizGoods1 != null) {
            for (SysBizGoodsDto bizGoods : sysBizGoods1) {
                SysBizGoodsSku sysBizGoodsSku = sysBizGoodsSkuMapper.selectSalesStockCount(bizGoods.getId());
                //实际销量
                bizGoods.setSalesCount(0L);
                //商品库存
                bizGoods.setStockCount(0L);
                if (sysBizGoodsSku != null) {
                    //实际销量
                    bizGoods.setSalesCount(sysBizGoodsSku.getSalesCount());
                    //商品库存
                    bizGoods.setStockCount(sysBizGoodsSku.getStockCount());
                }

            }
        }
        return sysBizGoods1;
    }
}
